/*
 * @Author       : Bing Chen chenbing@iocharger.com
 * @Date         : 2024-05-20 15:26:06
 * @LastEditors  : Bing Chen chenbing@iocharger.com
 * @LastEditTime : 2025-03-15 12:49:29
 * @FilePath     : \openchaoji-corelib\secc\include\OpenChaoJi_secc_hal.h
 * @Description  : 本文件定义SECC的HAL适配层接口
 *
 * Copyright (c) 2025 OpenChaoJi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the icense for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef __OPENCHAOJI_SECC_HAL_H__
#define __OPENCHAOJI_SECC_HAL_H__

#include "../../shared/include/OpenChaoJi_shared.h"

/**
 * 导引电路, IO驱动等
 * 洛阳嘉盛
*/


/**
SECC 相关接口
*/

/*******************************CAN通信接口*******************************/

/**
 * @brief  CAN接收
 * @param  handle: 句柄
 * @param  can_recv_info: can消息结构体
 * @return 0-接收成功 -1-接受失败
 */
int OpenChaoJi_secc_hal_information_recv_can_voluation(OpenChaoJi_hal_handle handle, struct can_pdu * can_recv_info);

/**
 * @brief  CAN发送
 * @param  handle: 句柄
 * @param  can_send_info: can消息结构体
 * @return 0-接收成功 -1-接受失败
 */
int OpenChaoJi_secc_hal_information_send_can_voluation(OpenChaoJi_hal_handle handle, const struct can_pdu * can_send_info);


/******************************控制继电器及反馈******************************/

/**
 * @brief  (A/B类系统)控制直流接触器
 * @param  handle: 句柄
 * @param  ctl: 0-断开 1-闭合
 * @return void
 * @remark C1\C2
 */
void OpenChaoJi_secc_hal_gbt_close_dc_relay(OpenChaoJi_hal_handle handle,unsigned char ctl);

/**
 * @brief  (A/B类系统)控制交流接触器
 * @param  handle: 句柄
 * @param  ctl: 0-断开 1-闭合
 * @return void
 */
void OpenChaoJi_secc_hal_gbt_close_ac_relay(OpenChaoJi_hal_handle handle,unsigned char ctl);

#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_A == CONFIG_YES)
/**
 * @brief  (A类系统)控制辅助电源供电
 * @param  handle: 句柄
 * @param  ctl: 0-断开 1-闭合
 * @return void
 */
void OpenChaoJi_secc_hal_gbt_a_close_aux_power(OpenChaoJi_hal_handle handle,unsigned char ctl);

/**
 * @brief  (A类系统)控制电子锁锁止
 * @param  handle: 句柄
 * @param  ctl: 0-断开 1-闭合
 * @return void
 */
void OpenChaoJi_secc_hal_gbt_a_lock(OpenChaoJi_hal_handle handle, unsigned char ctl);
#endif

#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_B == CONFIG_YES)
/**
 * @brief  (B类系统)控制S0
 * @param  handle: 句柄
 * @param  ctl: 0-断开 1-闭合
 * @return void
 */
void OpenChaoJi_secc_hal_gbt_b_close_s0(OpenChaoJi_hal_handle handle,unsigned char ctl);

/**
 * @brief  (B类系统)控制S1
 * @param  OpenChaoJi_hal_handle: 句柄
 * @param  ctl: 0-断开 1-闭合
 * @return void
 */
void OpenChaoJi_secc_hal_gbt_b_close_s1(OpenChaoJi_hal_handle handle,unsigned char ctl);
#endif

/**
 * @brief  (A/B类系统)获取直流接触器状态
 * @param  handle: 句柄
 * @return     C1   C2
 *         0   断   断
 *         1   断   通
 *         2   通   断
 *         3   通   通
 * @remark C1\C2
 */
#define OPENCHAOJI_SECC_HAL_DC_RELAY_C1C2_ALL_CLOSE 3
#define OPENCHAOJI_SECC_HAL_DC_RELAY_C1C2_ALL_OPEN  0
unsigned char OpenChaoJi_secc_hal_get_status_dc_relay(OpenChaoJi_hal_handle handle);

/**
 * @brief  (A/B类系统)获取交流接触器状态
 * @param  handle: 句柄
 * @return 0-断开 1-闭合
 */
bool OpenChaoJi_secc_hal_is_close_ac_relay(OpenChaoJi_hal_handle handle);

#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_A == CONFIG_YES)
/**
 * @brief  (A类系统)获取辅助电源供电
 * @param  handle: 句柄
 * @return 0-断开 1-闭合
 */
bool OpenChaoJi_secc_hal_gbt_a_is_closed_aux_power(OpenChaoJi_hal_handle handle);

/**
 * @brief  (A类系统)获取电子锁锁止
 * @param  handle: 句柄
 * @return 0-断开 1-闭合
 */
bool OpenChaoJi_secc_hal_gbt_a_is_locked(OpenChaoJi_hal_handle handle);

/**
 * @brief  (A类系统)获取泄放继电器状态
 * @param  handle: 句柄
 * @return 0-断开 1-闭合
 */
bool OpenChaoJi_secc_hal_gbt_a_is_closed_vent_relay(OpenChaoJi_hal_handle handle);
#endif

#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_B == CONFIG_YES)
/**
 * @brief  (B类系统)获取S0状态
 * @param  handle: 句柄
 * @return 0-断开 1-闭合
 */
bool OpenChaoJi_secc_hal_gbt_b_is_closed_s0(OpenChaoJi_hal_handle handle);

/**
 * @brief  (B类系统)获取S1状态
 * @param  handle: 句柄
 * @return 0-断开 1-闭合
 */
bool OpenChaoJi_secc_hal_gbt_b_is_closed_s1(OpenChaoJi_hal_handle handle);
#endif

/**
 * @brief  获取桩温度
 * @param  handle: 句柄
 * @return 桩温度
 */
int OpenChaoJi_secc_hal_gbt_get_box_temperature(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取枪温度
 * @param  handle: 句柄
 * @return 枪温度
 */
struct OpenChaoJi_secc_hal_plug_temp OpenChaoJi_secc_hal_obtain_information_connector_temperature(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取检测点1状态, A类系统和B类系统对检测点1的定义不同
 * @param  handle: 句柄
 * @return 检测点1状态
 * @remark 返回检测点1的检测电压值
 *
 */
int OpenChaoJi_secc_hal_gbt_get_vol_checkpoint_1(OpenChaoJi_hal_handle handle);

/******************************获取模块信息******************************/

/**
 * @brief  设置模块输出电流、输出电压
 * @param  handle: 句柄
 * @param  req_volt: 请求电压(0.1V)
 * @param  req_curr: 请求电流(0.1A)
 * @return void
 */
void OpenChaoJi_secc_hal_power_module_request_start(OpenChaoJi_hal_handle handle, unsigned int req_volt, unsigned int req_curr);

/**
 * @brief  设置模块关机
 * @param  handle: 句柄
 * @return void
 */
void OpenChaoJi_secc_hal_power_module_request_stop(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取模块当前开关机状态
 * @param  handle: 句柄
 * @return 开关机状态 0-关机 1-开机
 */
bool OpenChaoJi_secc_hal_power_module_is_started(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取模块当前输出电压
 * @param  handle: 句柄
 * @return 输出电压
 * @remark 单位0.1V, 返回值为实际数值的10倍、如700V则返回7000
 */
unsigned int OpenChaoJi_secc_hal_power_module_get_voltage(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取模块当前输出电流
 * @param  handle: 句柄
 * @return 输出电流
 * @remark 单位0.1A, 返回值为实际数值的10倍、如100A则返回1000
 */
unsigned int OpenChaoJi_secc_hal_power_module_get_current(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取充电机输出能力-最高充电电压
 * @param  handle: 句柄
 * @return 充电机最高充电电压
 * @remark 0.1V/位
 */
unsigned int OpenChaoJi_secc_hal_power_module_get_max_voltage(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取充电机输出能力-最小充电电压
 * @param  handle: 句柄
 * @return 最小输出电压
 * @remark 0.1V/位
 */
unsigned int OpenChaoJi_secc_hal_power_module_get_min_voltage(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取充电机输出能力-最大输出电流
 * @param  handle: 句柄
 * @return 最大输出电流
 * @remark 0.1A/位
 */
unsigned int OpenChaoJi_secc_hal_power_module_get_max_current(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取充电机输出能力-最小输出电流
 * @param  handle: 句柄
 * @return 最小输出电流
 * @remark 0.1A/位
 */
unsigned int OpenChaoJi_secc_hal_power_module_get_min_current(OpenChaoJi_hal_handle handle);

#if 0
/**
 * @brief  获取模块的额定功率
 * @param  OpenChaoJi_hal_handle: 句柄
 * @return 模块的额定功率
 * @remark 单位 默认返回值为 W,如20KW则返回20000
 */
unsigned int OpenChaoJi_secc_hal_power_module_get_rated_power(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取模块告警信息
 * @param  OpenChaoJi_hal_handle: 句柄
 * @return 模块告警信息
 */
unsigned int OpenChaoJi_secc_hal_obtain_information_module_warning(OpenChaoJi_hal_handle handle);

/**
 * @brief  获取模块故障信息
 * @param  OpenChaoJi_hal_handle: 句柄
 * @return 模块故障信息
 */
unsigned int OpenChaoJi_secc_hal_obtain_information_module_fault(OpenChaoJi_hal_handle handle);
#endif

/****** 此头文件嘉盛和长园共同维护, 为了避免合并冲突, 嘉盛仅修改本行以上部分, 长园只修改本行以下部分 ******/
/**
 * 电压电流采集
 * 长园
*/
// 电压电流采集
struct OpenChaoJi_secc_hal_samp {
    uint32_t voltage_in;            //内侧电压（0.1V/位）
    uint32_t voltage_out;           //外侧电压（0.1V/位）
    uint32_t current;               //电流（0.1A/位）
};
// 枪温采集
struct OpenChaoJi_secc_hal_plug_temp {
    uint32_t plug_p_temp;            // 正极枪温
    uint32_t plug_n_temp;           	// 负极枪温
};
/**
 * 自检, 包含粘连/短路/绝缘三个步骤
*/
// 自检参数
typedef enum{
    SELFSTA_ADHESION_NOTSTART = 0x00,   //粘连未检测
    SELFSTA_ADHESION_ING,               //粘连检测中
    SELFSTA_ADHESION_END,               //粘连检测结束
    SELFSTA_SHORT_NOTSTART,             //短路未检测
    SELFSTA_SHORT_ING,                  //短路检测中
    SELFSTA_SHORT_END,                  //短路检测结束
    SELFSTA_IMD_NOTSTART,               //绝缘未检测
    SELFSTA_IMD_ING,                    //绝缘检测中
    SELFSTA_IMD_END,                    //绝缘检测结束
}OpenChaoJi_secc_hal_self_status;

typedef enum{
    SELFRES_ADHESION_SUCCESS = 0x00,    //粘连检测成功
    SELFRES_ADHESION_ERROR,             //粘连检测失败
    SELFRES_SHORT_SUCCESS,              //短路检测成功
    SELFRES_SHORT_ERROR,                //短路检测失败
    SELFRES_IMD_SUCCESS,                //绝缘检测成功
    SELFRES_IMD_ERROR,                  //绝缘检测失败
}OpenChaoJi_secc_hal_self_result;

struct OpenChaoJi_secc_hal_selfdata{
    OpenChaoJi_secc_hal_self_status selfsta_adhesion;       //粘连检测状态
    OpenChaoJi_secc_hal_self_status selfsta_short;          //短路检测状态
    OpenChaoJi_secc_hal_self_status selfsta_imd;            //绝缘检测状态

    OpenChaoJi_secc_hal_self_result selfres_adhesion;       //粘连检测结果
    OpenChaoJi_secc_hal_self_result selfres_short;          //短路检测结果
    OpenChaoJi_secc_hal_self_result selfres_imd;            //绝缘检测结果
};
// HAL需要自己实现IMD的停止, 并且保留最后的阻值. 同时IMD需要支持多路同时进行, 如果HAL不支持多路, 应用层应当如何处理?
/**
 * @brief  启动阶段-自检启动接口
 * @param  handle: 句柄
 * @param  voltage: 绝缘电压值
 * @return  0-成功  -1-失败
 */
int OpenChaoJi_secc_hal_selfcheck_request_start(OpenChaoJi_hal_handle handle, uint32_t voltage);


/**
 * @brief  启动阶段-自检停止接口
 * @param  handle: 句柄
 * @return 0-成功  -1-失败
 */
int OpenChaoJi_secc_hal_selfcheck_request_stop(OpenChaoJi_hal_handle handle);


/**
 * @brief  启动阶段-自检结果获取
 * @param  handle: 句柄
 * @return  粘连/短路/绝缘 自检状态和结果
 */
struct OpenChaoJi_secc_hal_selfdata  OpenChaoJi_secc_hal_selfcheck_get_result(OpenChaoJi_hal_handle handle);

#if 0
/**
 * @brief  充电阶段-绝缘启动接口
 * @param  context: 句柄
 * @param  voltage: 绝缘电压值
 * @return  0:OK  -1:ERR
 */
int OpenChaoJi_secc_hal_imd_request_start(OpenChaoJi_hal_handle handle, uint32_t voltage);


/**
 * @brief  充电阶段-绝缘停止接口
 * @param  context: 句柄
 * @return  0:OK  -1:ERR
 */
int OpenChaoJi_secc_hal_imd_request_stop(OpenChaoJi_hal_handle handle);


/**
 * @brief  充电阶段-绝缘结果获取
 * @param  context: 句柄
 * @param  rp_value: 正对地阻值存储地址
 * @param  rn_value: 负对地阻值存储地址
 * @return  -1: ERR   0:OK  1:Checking
 */
int OpenChaoJi_secc_hal_imd_get_result(OpenChaoJi_hal_handle handle,
                                       uint32_t *rp_value, uint32_t *rn_value);
#endif

/**
 * @brief  泄放启动
 * @param  handle: 句柄
 * @return 0-成功  -1-失败
 */
int OpenChaoJi_secc_hal_vent_request_start(OpenChaoJi_hal_handle handle);


/**
 * @brief  泄放停止
 * @param  handle: 句柄
 * @return 0-成功  -1-失败
 */
int OpenChaoJi_secc_hal_vent_request_stop(OpenChaoJi_hal_handle handle);



/**
 * @brief  电压电流采样值获取
 * @param  handle: 句柄
 * @param  samp_value: 采样值存储地址
 * @return  0-成功  -1-失败
 */
int OpenChaoJi_secc_hal_sampling_value(OpenChaoJi_hal_handle handle,
                                       struct OpenChaoJi_secc_hal_samp *samp_value);


/**
 * @brief  获取急停按钮状态
 * @param  handle: 句柄
 * @return 0-断开 1-闭合
 * @remark 返回急停按钮状态
 *
 */
bool OpenChaoJi_secc_hal_is_emergency_pushed(OpenChaoJi_hal_handle handle);

#endif

